package org.csu.mypetstore.service;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.persistence.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountService {
    @Autowired
    private AccountMapper accountMapper;

    public Account getAccount(String username) {
        return accountMapper.getAccountByUsername(username);
    }

    public Account getAccount(String username, String password) {
        Account account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        return accountMapper.getAccountByUsernameAndPassword(account);
    }

    public void insertAccount(Account account) {
        accountMapper.insertAccount(account);
        accountMapper.insertProfile(account);
        accountMapper.insertSignon(account);
    }

    public void updateAccount(Account account) {
        accountMapper.updateAccount(account);
        accountMapper.updateProfile(account);

        if (account.getPassword() != null && account.getPassword().length() > 0) {
            accountMapper.updateSignon(account);
        }
    }

    //后台
    //查询所有的用户id
    public List<String> getAccountList(){
        return accountMapper.getUsernameList();
    }

    //查询用户基本信息：account、profile
    public Account getCommonInformation(String username){
        return accountMapper.getCommonInformation(username);
    }

    //查询用户安全信息：密码、手机、邮箱
    public Account getSafeInformation(String username){
        return accountMapper.getSafeInformation(username);
    }

    //修改用户基本信息：account、profile
    public void updateCommonInformation(Account account){
        accountMapper.updateCommonInformation(account);
        accountMapper.updateProfile(account);
    }

    //修改用户安全信息：密码、手机、邮箱
    public void updateSafeInformation(Account account){
        accountMapper.updateSafeInformation(account);
        accountMapper.updatePassword(account);
    }
}
